﻿var tset = document.getElementById('tset');
var thistory = document.getElementById('thistory');
var tabout = document.getElementById('tabout');
var aset = document.getElementById('aset');
var ahistory = document.getElementById('ahistory');
var aabout = document.getElementById('aabout');
aset.style.background = 'url(li.png) top right';

var rule = document.getElementById('rule');
var msg = document.getElementById('msg');
var rulelist = document.getElementById('rulelist');
var options = rulelist.options;

function add(){
	var value = rule.value;
	if(value.length != 0){
		if(localStorage.rules.indexOf(value)==-1){
			if(localStorage.rules != ''){ localStorage.rules += ','; }
			localStorage.rules += value;
			loadlist();
		}else{
			msg.textContent='此规则已经存在！';
		}
	}
}

function loadlist(){
	if(localStorage.rulelist != ''){
		var array= new Array();
		array = localStorage.rules.split(',');
		for(var i=array.length-1; i>=0; i--){
			var option=document.createElement('option')
			option.textContent=array[i];
			rulelist.appendChild(option);
		}
		document.getElementById('msg').textContent = '共 ' + array.length + ' 条规则';
	}
}

function savelist(){
	localStorage.rules='';
	var ol=options.length;
	for(var i=0; i<ol; i++){
		localStorage.rules += options[i].text;
		if( i<ol-1 ) localStorage.rules += ',';
	}
	msg.textContent='已保存';
	rule.value='';
}

document.getElementById('add').addEventListener('click', function(){ msg.textContent='';add(); },false);

document.getElementById('save').addEventListener('click', function(){ savelist(); },false);

document.getElementById('del').addEventListener('click', function(){
	rulelist.removeChild(rulelist.options[rulelist.selectedIndex]);
	msg.textContent='';
},false);

aset.addEventListener('click', function(){
	this.style.background='url(li.png) top right';
	ahistory.style.background='none';
	aabout.style.background='none';
	tset.style.display='block';
	thistory.style.display='none';
	tabout.style.display='none';
},false);

ahistory.addEventListener('click', function(){
	this.style.background='url(li.png) top right';
	aset.style.background='none';
	aabout.style.background='none';
	tset.style.display='none';
	thistory.style.display='block';
	tabout.style.display='none';
},false);

aabout.addEventListener('click', function(){
	this.style.background='url(li.png) top right';
	aset.style.background='none';
	ahistory.style.background='none';	
	tset.style.display='none';
	thistory.style.display='none';
	tabout.style.display='block';
},false);

rulelist.addEventListener('dblclick', function(){
	this.removeChild(this.options[this.selectedIndex]);
	msg.textContent='';
},false);

loadlist();
